package GUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Toolkit;
import java.io.IOException;

import javax.swing.JTextField;
import javax.swing.JButton;

/**
 *@author Juan Alapin - 324507516
 *@author Giora Yaffe - 036551281
 *@author Shachar Gritzman - 300174026
 *@author Nir Porat - 021515580
*/

public class ClientGUI extends JFrame implements ActionListener {
	
	private static String juanIP = "79.179.68.210";
	private JButton btnLogin;
	private MainGUI maingui;
	
	
	public ClientGUI(){
	}
	
	public ClientGUI(MainGUI maingui) {
		super(); 
		this.maingui= maingui;
		getContentPane().setLayout(null);
		
		JLabel lblIcmClient = new JLabel("ICM Client");
		lblIcmClient.setFont(new Font("Tahoma", Font.PLAIN, 16));
		lblIcmClient.setBounds(162, 11, 105, 29);
		getContentPane().add(lblIcmClient);
		
		JLabel lblEnterIp = new JLabel("IP:");
		lblEnterIp.setBounds(53, 75, 46, 14);
		getContentPane().add(lblEnterIp);
		
		textField = new JTextField("localhost");
		textField.setBounds(153, 72, 139, 20);
		getContentPane().add(textField);
		textField.setColumns(10);
		
		JLabel lblPort = new JLabel("Port:");
		lblPort.setBounds(53, 110, 46, 14);
		getContentPane().add(lblPort);
		
		textField_1 = new JTextField("5555");
		textField_1.setBounds(153, 107, 139, 20);
		getContentPane().add(textField_1);
		textField_1.setColumns(10);
		
		btnLogin = new JButton("Login");
		btnLogin.setBounds(157, 165, 89, 23);
		btnLogin.addActionListener(this);
		add(btnLogin);
		
		
		this.setVisible(true);
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField textField;
	private JTextField textField_1;

	/**
	 * @param args
	 */
	public String getIP() {
		if(textField.getText() == "juan"){
			return juanIP;
		}
		else 
			return new String (textField.getText());
	}
	public int getPort() {
		return Integer.parseInt(textField_1.getText());
		}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource()== btnLogin) {
				try {
					maingui.clientHandler();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} 
				close();
			}
		
		
	}

	public void close(){ // Let us close a window
		WindowEvent winClosingEvent = new WindowEvent(this,WindowEvent.WINDOW_CLOSING);
		Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(winClosingEvent);
	}

}
